<script lang="ts" setup>
  import { computed, onMounted, ref } from 'vue'
  import { useRoute, useRouter } from 'vue-router'

  import { previewStructuredFile } from '/@/api/exchange/requirement'

  const router = useRouter()
  const route = useRoute()
  const fileList = route.query.fileString ? JSON.parse(route.query.fileString) : []
  const list = ref<any[]>([])
  const currentList = ref<number[]>([])
  const totalList = ref<number[]>([])

  // 分页查询
  const handleTableChange: TableProps['onChange'] = (tabPage: TablePaginationConfig, index: number) => {
    const id = fileList[index].fileId
    currentList.value[index] = tabPage - 0
    structuredPreviewFile(id, index, 'changePage')
  }

  // 结构化数据预览
  const structuredPreviewFile = async (id: string, index: number, type?: string) => {
    const { responseCode, responseData } = await previewStructuredFile({
      id: id,
      sheetNo: 0,
      previewOffset: (currentList.value[index] - 1) * 10,
      previewRows: 10,
    })
    if (responseCode === 100000000 && responseData.data) {
      if (type) {
        list.value[index] = responseData.data
      } else {
        list.value.push(responseData.data)
        totalList.value.push(responseData.data.totalRows)
      }
    }
  }

  /**回退页面*/
  const pageBack = () => {
    router.back()
  }

  onMounted(() => {
    if (fileList) {
      fileList.map((item, index) => {
        currentList.value.push(1)
        structuredPreviewFile(item.fileId, index)
      })
    }
  })
</script>

<template>
  <div class="data-detail">
    <div class="data-detail-header">
      <div>数据详情</div>
      <a-button type="primary" @click="pageBack">返回</a-button>
    </div>
    <template v-if="list.length">
      <div class="data-list-box" v-for="(item, index) in list" :key="index">
        <div class="data-title">{{ fileList[index].fileName }}</div>
        <div class="data-list">
          <div class="data-list-item" v-if="item.headers.length">
            <div class="data-list-column" v-for="ite in item.headers" :key="ite">{{ ite }}</div>
          </div>
          <div class="data-list-body" v-if="item.data.length">
            <div class="data-list-item" v-for="(item1, index1) in item.data" :key="index1">
              <div class="data-list-column" v-for="ite in item1" :key="ite">{{ ite }}</div>
            </div>
          </div>
        </div>
        <div class="data-list-pagination">
          <a-pagination v-model:current="currentList[index]" :defaultPageSize="10" :total="totalList[index]" show-less-items @change="(tabPage) => handleTableChange(tabPage, index)" />
        </div>
      </div>
    </template>
  </div>
</template>

<style lang="less" scoped>
  .data-detail {
    width: 100%;
    padding: 0 24px 30px;
    background: #fff;
    border-radius: 2px;

    .data-detail-header {
      width: 100%;
      height: 64px;
      margin-bottom: 20px;
      border-bottom: 1px solid #e2e3e5;
      font-size: 16px;
      font-weight: bold;
      color: #000;
      line-height: 64px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .data-list-box {
      width: 100%;
      border: 1px solid #e2e3e5;
      margin-bottom: 20px;
      border-radius: 12px;

      .data-title {
        color: #000;
        border-bottom: 1px solid #e2e3e5;
        padding: 10px;
      }

      .data-list {
        max-width: calc(100% - 30px);
        width: max-content;
        height: max-content;
        margin: 15px;
        padding: 0;
        border-bottom: 1px solid #e2e3e5;
        border-left: 1px solid #e2e3e5;
        border-right: 1px solid #e2e3e5;
        overflow: hidden;
        overflow-x: scroll;

        .data-list-body {
          padding: 0;
          margin: 0;

          .data-list-item {
            background: #fff;
          }
        }

        .data-list-item {
          display: flex;
          height: 40px;
          padding: 0;
          margin: 0;
          background: #f2f3f5;
          border-top: 1px solid #e2e3e5;
        }

        .data-list-column {
          min-width: 100px;
          padding: 0;
          margin: 0;
          line-height: 30px;
          text-align: center;
          color: #000;
        }
      }

      .data-list-pagination {
        margin: 20px 0 20px 20px;
      }
    }
  }
</style>
